﻿namespace PLCS.Domain.Processes;

public class Process : BaseEntity
{
    /// <summary>
    /// 工序编号
    /// </summary>
    public string ProcessNo { get; init; }

    /// <summary>
    /// 工序名称
    /// </summary>
    public string ProcessName { get; set; }

    /// <summary>
    /// 目标位(可以有多个，用‘，’分割)
    /// </summary>
    public string TargetPos { get; set; }

    /// <summary>
    /// 承接工序名称(可为空，为空即为按步骤往下走，有值的话，做完当前工序要去寻找承接工序的那一步，从那开始执行)
    /// </summary>
    public string? ContinueProcess { get; set; }

    /// <summary>
    /// 是否有触发条件
    /// </summary>
    public bool IsTrigger { get; set; }

    /// <summary>
    /// 是否下线
    /// </summary>
    public YesNo IsEnd { get; set; }

    /// <summary>
    /// 触发条件集合（其实就是多对多，只不过设置外键容易出错）
    /// </summary>
    public string? TriggerStrings { get; set; }

    /// <summary>
    /// 工步（其实就是多对多，只不过设置外键容易出错）
    /// </summary>
    public string? WorkStepStrings { get; set; }

    /// <summary>
    /// 任务前执行方法
    /// </summary>
    public string? BeforeExecutionMethod { get; set; }

    /// <summary>
    /// 任务前执行参数
    /// </summary>
    public string? BeforeExecutionParam { get; set; }

    /// <summary>
    /// 任务完成执行方法
    /// </summary>
    public string? AfterExecutionMethod { get; set; }

    /// <summary>
    /// 任务完成执行参数
    /// </summary>
    public string? AfterExecutionParam { get; set; }

    /// <summary>
    /// 触发条件
    /// </summary>
    ///
    [NotMapped]
    public List<Trigger>? Triggers { get; set; }

    public Process()
    {
    }

    public Process(string processNo, string processName, string targetPos, YesNo isEnd, string? continueProcess = null, string? beforeExecutionMethod = null, string? beforeExecutionParam = null, string? afterExecutionMethod = null, string? afterExecutionParam = null)
    {
        ProcessNo = processNo;
        ProcessName = processName;
        TargetPos = targetPos;
        ContinueProcess = continueProcess;
        IsEnd = isEnd;
        BeforeExecutionMethod = beforeExecutionMethod;
        BeforeExecutionParam = beforeExecutionParam;
        AfterExecutionMethod = afterExecutionMethod;
        AfterExecutionParam = afterExecutionParam;
    }

    public Process AddTrigger(Trigger trigger)
    {
        if (!IsTrigger)
            IsTrigger = true;
        if (string.IsNullOrEmpty(TriggerStrings))
            TriggerStrings += $"{trigger.Id}";
        else
            TriggerStrings += $"{ConstantChar.SplitChar}{trigger.Id}";
        return this;
    }

    public Process AddTriggers(List<Trigger> triggers)
    {
        if (!IsTrigger)
            IsTrigger = true;
        foreach (var item in triggers)
        {
            if (string.IsNullOrEmpty(TriggerStrings))
                TriggerStrings += $"{item.Id}";
            else
                TriggerStrings += $"{ConstantChar.SplitChar}{item.Id}";
        }
        return this;
    }

    public Process RemoveTrigger(Trigger trigger)
    {
        if (!TriggerStrings.IsNullOrEmpty())
        {
            if (TriggerStrings.Contains(trigger.Id.ToString()))
            {
                TriggerStrings.Replace($"{ConstantChar.SplitChar}{trigger.Id}", "");
                TriggerStrings.Replace($"{trigger.Id}", "");
            }
        }
        if (TriggerStrings.IsNullOrEmpty() && IsTrigger)
            IsTrigger = false;

        return this;
    }
}